#include "SpriteText.h"


SpriteText::SpriteText()
: Sprite()
, font(nullptr)
, size(12)
, style(0)
, isVisible(true)
, path(nullptr)
, message(nullptr)
{
}

SpriteText::SpriteText(const std::string& path, int style, const std::string& message)
: Sprite()
, font(0)
, size(12)
, style(style)
, isVisible(true)
, path(path)
, message(message)
{
	PrintTextTexture(message);
}

SpriteText::SpriteText(const std::string& path, int style, const std::string& message, int x, int y)
: Sprite()
, font(0)
, size(12)
, style(style)
, isVisible(true)
, path(path)
, message(message)
{
	PrintTextTexture(message);
	SetPosition(x, y);
}

SpriteText::SpriteText(const std::string& path, int style, const std::string& message, Vector* position)
: Sprite()
, font(0)
, size(12)
, style(style)
, isVisible(true)
, path(path)
, message(message)
{
	PrintTextTexture(message);
	SetPosition(position);
}

SpriteText::SpriteText(const std::string& path, int style, const std::string& message, int size)
: Sprite()
, font(0)
, size(size)
, style(style)
, isVisible(true)
, path(path)
, message(message)
{
	PrintTextTexture(message);
}

SpriteText::SpriteText(const std::string& path, int style, const std::string& message, int size, int x, int y)
: Sprite()
, font(0)
, size(size)
, style(style)
, isVisible(true)
, path(path)
, message(message)
{
	PrintTextTexture(message);
	SetPosition(x, y);
}

SpriteText::SpriteText(const std::string& path, int style, const std::string& message, int size, Vector* position)
: Sprite()
, font(0)
, size(size)
, style(style)
, isVisible(true)
, path(path)
, message(message)
{
	PrintTextTexture(message);
	SetPosition(position);
}

SpriteText::SpriteText(const std::string& path, int style, const std::string& message, int size, SDL_Color color)
: Sprite()
, font(0)
, color(color)
, size(size)
, style(style)
, isVisible(true)
, path(path)
, message(message)
{
	PrintTextTexture(message);
}

SpriteText::SpriteText(const std::string& path, int style, const std::string& message, int size, SDL_Color color, int x, int y)
: Sprite()
, font(0)
, color(color)
, size(size)
, style(style)
, isVisible(true)
, path(path)
, message(message)
{
	PrintTextTexture(message);
	SetPosition(x, y);
}

SpriteText::SpriteText(const std::string& path, int style, const std::string& message, int size, SDL_Color color, Vector* position)
: Sprite()
, font(0)
, color(color)
, size(size)
, style(style)
, isVisible(true)
, path(path)
, message(message)
{
	PrintTextTexture(message);
	SetPosition(position);
}

void SpriteText::PrintTextTexture(const std::string& message)
{
	if (font == nullptr)
	{
		font = Resources::GetInstance()->GetFont(path, size);
	}
	TTF_SetFontStyle(font, style);
	SDL_Texture* textTexture = CreateTexture(message, color);
	SetTexture(textTexture, GetRect());
}

SDL_Texture* SpriteText::CreateTexture(const std::string& message, SDL_Color color)
{
	SDL_Surface* surf = TTF_RenderText_Blended(font, message.c_str(), color);
	if (surf == nullptr){
		TTF_CloseFont(font);
		std::cout << "Failed to create surface: " << surf << " -- " << TTF_GetError();
		return nullptr;
	}
	SDL_Renderer* renderer = Engine::GetInstance()->GetRenderer();
	SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surf);
	if (texture == nullptr){
		std::cout << "Failed to create text texture: " << texture << " -- " << TTF_GetError();
	}
	//Clean up the surface and font
	SDL_FreeSurface(surf);
	return texture;
}

SpriteText::~SpriteText()
{
	TTF_CloseFont(font);
	size = 0;
	style = 0;
	isVisible = false;
	TTF_CloseFont(font);
	tex = 0;
	path = nullptr;
	message = nullptr;
}
